#include "core/main.h"

void SarsaCoordinator::takeStep()
{
    _gameAgent->takeStep();
    
    _curFrame++;
    if (_curFrame == 1000) {
        mike::MatrixWriterAppendDouble("TotalTime", _gameAgent->getTotalComputeTime());
        mike::MatrixWriterWriteToFileDouble("TotalTime", "../computeTime.txt");
    }

    const Game* game = _gameAgent->getGame();

    stringstream ss;
    ss << game->ActiveFrame().observedInts[5];
    mike::GlTextConsolePrint(game->ObservedIntNames()[5], ss.str());
    ss.str("");

    ss << game->ActiveFrame().observedInts[6];
    mike::GlTextConsolePrint(game->ObservedIntNames()[6], ss.str());
    ss.str("");

    if (game->ActiveFrame().observedInts[0] == 1) {
        _curEpisodes++;

        mike::MatrixWriterAppendInt(game->ObservedIntNames()[1], game->ActiveFrame().observedInts[1]);
        mike::MatrixWriterAppendInt(game->ObservedIntNames()[5], game->ActiveFrame().observedInts[5]);
        mike::MatrixWriterAppendInt(game->ObservedIntNames()[6], game->ActiveFrame().observedInts[6]);

        if (_curEpisodes == _maxEpisodes) {
            stringstream name1;
            name1 << "../" << game->ObservedIntNames()[1] << ".txt";
            mike::MatrixWriterWriteToFileInt(game->ObservedIntNames()[1], name1.str());

            stringstream name5;
            name5 << "../" << game->ObservedIntNames()[5] << ".txt";
            mike::MatrixWriterWriteToFileInt(game->ObservedIntNames()[5], name5.str());
            
            stringstream name6;
            name6 << "../" << game->ObservedIntNames()[6] << ".txt";
            mike::MatrixWriterWriteToFileInt(game->ObservedIntNames()[6], name6.str());

            //mike::MatrixWriterWriteToFileDouble("delta", "../delta.txt");
        }
    }

    ss << _curEpisodes << " / " << _maxEpisodes;
    mike::GlTextConsolePrint("Current Episode", ss.str());
    ss.str();
}
